<template>
	<view class="comment">
		<u-navbar title="商品评论" :background="{ background: '#a0cfff' }" title-color="#FFFFFF"></u-navbar>
		<view class="comCon u-p-30">
			<view class="conBox u-p-20">
				<view class="conSin flex-sbc">
					<view class="left">
						<image :src="shopUrl" style="width: 150rpx;height: 150rpx;border-radius: 10rpx;"></image>
					</view>
					<view class="right">
						<view class="u-line-2">商品：{{title}}</view>
						<view  style="color: #DADBDE;">
							<text class="u-font-20">品质：{{lityType}}成新</text>
						</view>
					</view>
				</view>
				<view class="scror u-m-t-30 u-m-b-30">
					<text class="u-font-30">描述相符：</text>
					<u-rate v-model="value" size="42"></u-rate>
				</view>
				<view>
					<u-input type="textarea" v-model="content" height="250"/>
				</view>
				<view class="u-m-t-30 u-m-b-30">
					<u-upload ref="uUpload" :action="action" :file-list="fileList" width="160" height="160"
						@on-remove="onRemove" @on-Success="onSuccess"></u-upload>
				</view>
				<view class="u-m-t-30 u-m-b-30">
					<u-button type="error" @click="sumitClick()">提交评论</u-button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import helper from '../../common/helper.js'
	export default {
		data() {
			return {
				id: '',
				index: '',
				shopId: '',
				title: '',
				shopUrl: '',
				lityType: '',
				value: 5,
				content: '',
				action: 'http://localhost:9000/eduoss/imgFile',
				fileList: [],
				fileImage: [],
				sellerId: ""
			}
		},
		onLoad: function(options) {
			let that = this
			// console.log(options)
			if (options.id != "") {
				that.id = options.id
			}
			if (options.shopId != "") {
				that.shopId = options.shopId
			}
			if (options.index != "") {
				that.index = options.index
			}
			if (options.title != "") {
				that.title = options.title
			}
			if (options.shopUrl != "") {
				that.shopUrl = options.shopUrl
			}
			if (options.lityType != "") {
				that.lityType = options.lityType
			}
			if (!options.sellerId && options.sellerId != "") {
				that.sellerId = options.sellerId
			}
			
		},
		methods: {
			sumitClick(){
				// this.orderClick(this.id,this.index,this.shopId)
				this.commentClick()
			},
			commentClick(){
				let that = this
				let user = uni.getStorageSync("user")
				var file = []
				that.fileImage.forEach((item, index) => {
					file.push(item.url)
				})
				let data = {
					shopId: that.shopId,
					userId: user.id,
					orderId: that.id,
					sellerId: that.sellerId,
					content: that.content,
					score: that.value,
					commentUrl: file.join("<<tupian>>")
				}
				that.postRequest(helper.addComment, data).then(res => {
					if (res.data.flag == "200") {
						uni.showToast({
							title: '评论成功',
							icon: 'none',
							duration: 2000
						})
						that.orderClick(this.id,this.index,this.shopId)
					} else {
						uni.showToast({
							title: '评论失败',
							icon: 'none',
							duration: 2000
						})
					}
				})
			},
			orderClick(id, index, shopId) {
				let that = this
				let data = {
					id: id,
					orderType: index,
					shopId: shopId
				}
				that.postRequest(helper.updateOrderType, data).then(res => {
					if (res.data.flag == "200") {
						uni.redirectTo({
							url: '../me/order?cunt=3'
						})
					} else {
						
					}
				})
			},
			onSuccess(data, index, lists) {
				const that = this
				let currentFile = [];
				// console.log(data,lists)
				lists.forEach((item, index) => {
					if (item.response) {
						let obj = {
							url: item.response.collectNum
						}
						currentFile.push(obj)
					} else {
						let link = item.url
						let obj = {
							url: link
						}
						currentFile.push(obj)
					}
				})
				that.fileImage = currentFile
			},
			onRemove(data, lists) {
				const that = this
				if (lists.length > 0) {
					var currentfileList = [];
					lists.forEach((item, index) => {
						if (item.response) {
							let obj = {
								url: item.response.collectNum
							}
							currentfileList.push(obj)
						} else {
							let link = item.url
							let obj = {
								url: link
							}
							currentfileList.push(obj)
						}
					})
					that.fileImage = currentfileList
				} else {
					that.fileImage = []
				}
			}
		}
	}
</script>

<style>
	page {
		background-color: rgb(240, 240, 240);
	}
</style>
<style lang="scss" scoped>
.comment{
	.comCon{
		.conBox{
			background-color: #FFFFFF;
			border-radius: 20rpx;
			.conSin{
				.left{
					width: 160rpx;
					// height: 150rpx;
				}
				.right{
					width: calc(100% - 160rpx);
				}
			}
		}
	}
}
</style>
